import 'package:get/get.dart';
import 'package:hive/hive.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';

import 'app/core/theme/customize.dart';
import 'app/data/services/hive_service.dart';
import 'app/data/services/theme_service.dart';
import 'app/core/bindings/application_bindings.dart';
import 'app/core/utils/adapt.dart';
import 'app/routes/app_pages.dart';

void main() async {
  // 确保 Flutter 引擎已经被正确初始化
  WidgetsFlutterBinding.ensureInitialized();

  // 初始化全局服务
  await Get.putAsync(() => HiveService().init());
  Get.put<ThemeService>(ThemeService());

  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    // 初始化屏幕适配工具
    Adapt.init(context);
    // App重新启动时默认锁定屏幕
    Hive.box(name: "lock").put("isLock", true);

    return GetMaterialApp(
      title: 'PassLog',
      theme: ThemeService.to.isDarkMode
          ? const MaterialTheme(TextTheme()).dark()
          : const MaterialTheme(TextTheme()).light(),
      initialBinding: ApplicationBindings(),
      unknownRoute: AppPages.unknownRoute,
      initialRoute: AppPages.initial,
      getPages: AppPages.routes,
      builder: EasyLoading.init(), // 初始化 EasyLoading 组件
    );
  }
}
